package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.StudentAttendance;
import com.example.result.Result;
import com.example.service.StudentAttendanceService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 学生考勤控制层
 * @date 2020-02-23
 */
@RestController
@RequestMapping("/api")
public class StudentAttendanceController {

    @Autowired
    StudentAttendanceService studentAttendanceService;

    // ===============================学生考勤管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加学生考勤信息
     */
    @RequestMapping(value = "/studentAttendances", method = RequestMethod.POST)
    public Result addStudentAttendance(@RequestBody String json) {
        // 将JSON数据转换为StudentAttendance对象实体
        JSONObject parseObject = JSON.parseObject(json);
        StudentAttendance studentAttendance = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), StudentAttendance.class);

        return studentAttendanceService.addStudentAttendance(studentAttendance);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新学生考勤信息
     */
    @RequestMapping(value = "/studentAttendances", method = RequestMethod.PUT)
    public Result updateStudentAttendance(@RequestBody String json) {
        // 将JSON数据转换为StudentAttendance对象实体
        JSONObject parseObject = JSON.parseObject(json);
        StudentAttendance studentAttendance = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), StudentAttendance.class);
        return studentAttendanceService.updateStudentAttendance(studentAttendance);

    }

    /**
     * @param studentAttendanceId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据学生考勤编号删除学生考勤信息
     */
    @RequestMapping(value = "/studentAttendances/{studentAttendanceId}", method = RequestMethod.DELETE)
    public Result deleteStudentAttendanceById(@PathVariable("studentAttendanceId") String studentAttendanceId) {
        return studentAttendanceService.deleteStudentAttendanceById(studentAttendanceId);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有学生考勤信息
     */
    @RequestMapping(value = "/studentAttendances", method = RequestMethod.GET)
    public Result queryAllStudentAttendance() {
        return studentAttendanceService.queryAllStudentAttendance();

    }

    /**
     * @param studentAttendanceId
     * @return 以JSON格式返回该学生考勤信息
     * @describe 根据编号查询学生考勤信息
     */
    @RequestMapping(value = "/studentAttendances/{studentAttendanceId}", method = RequestMethod.GET)
    public Result queryStudentAttendanceById(@PathVariable("studentAttendanceId") String studentAttendanceId) {
        return studentAttendanceService.queryStudentAttendanceById(studentAttendanceId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加学生考勤信息
     */
    @RequestMapping(value = "/studentAttendances/excels", method = RequestMethod.POST)
    public Result addStudentAttendanceByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<StudentAttendance> list = JSON.parseArray(parseObject.getString("json"), StudentAttendance.class);

        System.out.println(list);

        return studentAttendanceService.addStudentAttendanceByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据学生考勤编号批量删除学生考勤信息
     */
    @RequestMapping(value = "/studentAttendances/dels", method = RequestMethod.POST)
    public Result deleteStudentAttendanceByIds(@RequestBody Map<String, String> Ids) {

        return studentAttendanceService.deleteStudentAttendanceByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 学生考勤信息模糊查询
     */
    @RequestMapping(value = "/studentAttendances/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return studentAttendanceService.serachByInfo(info);

    }
}
